package com.youlu.campus.web.user.controller;

import com.youlu.campus.entity.VO.RankingListQueryVO;
import com.youlu.campus.entity.VO.req.UserPointsQuerVO;
import com.youlu.campus.service.platform.PointPlatformConfigService;
import com.youlu.campus.service.ranking.UserPointRankingService;
import com.youlu.campus.service.visitor.UserPointDailySignInService;
import com.youlu.campus.service.visitor.UserPointsService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 用户积分
 */
@RestController
public class UserPointController {
    @Autowired
    private UserPointsService userPointsService;
    @Autowired
    private UserPointDailySignInService userPointDailySignInService;
    @Autowired
    private UserPointRankingService userPointRankingService;
    @Autowired
    private PointPlatformConfigService pointPlatformConfigService;

    /**
     * 我的积分
     *
     * @return
     */
    @PostMapping("/mini-app/user-point/my")
    public QResult myPointInfo() {

        return QResult.success(userPointsService.getMyPoint(RequestContext.getUserId(), RequestContext.getAppId()));
    }

    /**
     * 积分明细
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/user-point/list")
    public QResult getPointDetail(@RequestBody UserPointsQuerVO req) {
        req.setUserId(RequestContext.getUserId());
        req.setAppId(RequestContext.getAppId());
        req.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(userPointsService.list(req));
    }

    /**
     * 积分签到
     *
     * @return
     */
    @PostMapping("/mini-app/user-point/sign-in")
    public QResult signIn() {
        boolean re = userPointDailySignInService.signIn(RequestContext.getCurUser(),RequestContext.getAppId(), new Date());
        if (re) {
            userPointRankingService.updatePoint(RequestContext.getCurUser(), "", pointPlatformConfigService.getPoint("sign-up"));
        }
        return QResult.success(re);
    }

    /**
     * 积分排行榜
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/user-point/rank-list")
    public QResult getPointDetail(@RequestBody RankingListQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(userPointRankingService.getUserTaskRanking(req, RequestContext.getCurUser()));
    }
}
